#include "threadPool.h"

// 处理ls命令，即展示当前目录下所有文件
int ls_work(const client_info_t *client_info)
{
	char currentDir[1024];
	strcpy(currentDir, client_info->global_path);
	printf("该用户当前所在目录为：%s\n", currentDir);
	

	DIR *dir = opendir(currentDir);
	ERROR_CHECK(dir, NULL, "open");
	char listname[1024] = {0};
	struct dirent *pdirent;
	while ((pdirent = readdir(dir)) != NULL)
	{
		if (strcmp(pdirent->d_name, ".") == 0 || strcmp(pdirent->d_name, "..") == 0)
		{
			continue;
		}
		strcat(listname, pdirent->d_name);
		strcat(listname, "/");
	}
	int len = strlen(listname);
	if (len > 0)
	{
		listname[len - 1] = '\0';
	}

	train_t train;
	memset(&train, 0, sizeof(train));
	train.len = strlen(listname);
	memcpy(train.data, listname, strlen(listname));
	send(client_info->netfd, &train, sizeof(train.len) + train.len, MSG_NOSIGNAL);

	closedir(dir);

	return 0;
}
